package cn.wang.heng.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.wang.heng.make.GoodsSingle;

public class dingdan extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public dingdan() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String path = request.getContextPath();

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String nname=request.getParameter("nname");
    	String npass=request.getParameter("npass");
    	String sum1=request.getParameter("sum");
    	float sum=Float.parseFloat(sum1);
    	
    	String name=nname;
    	String pass=npass;
    	
    	
    	try{
    		HttpSession session =request.getSession();
        	GoodsSingle user=new GoodsSingle();
    		user.setName(nname);
    		user.setPass(npass);
    		user.setPrice(sum);
    		session.setAttribute("user", user);
    		//out.print("111");
    		
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shopping?characterEncoding=utf8","root","123456");
			String rs = "select uname,upassword from tb_member";
			
			Statement stmt=con.createStatement();
			ResultSet rs12=stmt.executeQuery("select * from tb_member");
			
			
			
	//		PreparedStatement stm = con.prepareStatement(rs);
	///		stm.setString(1, name);
	//		stm.setString(2, pass);
	//		out.print("111");
	//		ResultSet rs1=null;
	//		out.print("111");
	//		rs1=stm.executeQuery();
	//		out.print("111");
			
			int a=0;
			while(rs12.next())
			{
				a++;
				if(name.equals(rs12.getString(2))  && pass.equals(rs12.getString(3)) ){
					response.sendRedirect(path+"/aaa.jsp");
				}
				
			}
			if(a==0)
			{
				response.sendRedirect(path+"/ding.jsp");
			}
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
